<template>
	<view class="scroll">
		<scroll-view  ref="scrollRef" style="height: 100%" :scroll-top="scrollTop"  @scroll="scroll" scroll-y scroll-with-animation enable-back-to-top refresher-enabled>
			<view id="scroll-view">
				<view class="news-item" v-for="item of list" :key="item._id" :class="item.sender._id == userInfo._id ? 'self' : ''">
					<view class="avatar">
						
					</view>
					<view class="news-content">
						<view class="news-sender" v-if="item.sender._id != userInfo._id">{{ item.sender.name }}</view>
						<view class="news-text">{{ item.text }}</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
	<view class="chat-footer">
		<uni-easyinput v-model="chatValue" clearable confirmType="send" :trim="true" @confirm="sendMsg"></uni-easyinput>
	</view>
</template>

<script setup lang="ts">
	import { ref, onMounted, computed, nextTick } from 'vue';
	import request from '../../common/request';
	
	const userInfo = uni.getStorageSync('userInfo')
	const convChat = uni.getStorageSync('chat-conv')
	
	let crowd = computed(() => {
		return convChat.crowd.map(it => it._id).filter(id => id != userInfo._id)
	})
	
	const scrollTop = ref(0)
	const scrollHeight = ref(0)
	const oldScrollTop = ref(0)
	const chatValue = ref('')
	
	const list = ref([])
	async function convNews() {
		const { code, data } = await request.post('/convNews', { convId: convChat._id, lastTime: convChat.lastTime })
		if (code) {
			list.value = data
		}
	}
	const scroll = async (e) => {
		oldScrollTop.value = e.detail.scrollTop
		scrollHeight.value = e.detail.scrollHeight
	}
	async function goBottom() {
		scrollTop.value = oldScrollTop.value
		await nextTick()
		scrollTop.value = scrollHeight.value
	}
	const sendMsg = async () => {
		const { code } = await request.post('/sendMsg', { convId: convChat._id, text: chatValue.value })
		if (code) {
			let obj = {
				text: chatValue.value,
				convId: convChat._id,
				sender: {
					_id: userInfo._id,
					name: userInfo.name
				}
			}
			chatValue.value = ''
			list.value.push(obj)
			request.ws('/sendMsg', {
				userIds: crowd.value,
				msg: obj
			})
			goBottom()
		}
	}
	
	onMounted(async () => {
		await convNews()
		
		await nextTick()
		const query = uni.createSelectorQuery().in(this);
		query
		  .select("#scroll-view")
		  .boundingClientRect((data) => {
			scrollTop.value = data['height']
		  })
		  .exec();

		
		request.onWs((value) => {
			if (value.url == '/sendMsg') {
				console.log('ws/sendMsg', value.data)
				if (value.data.code) {
					list.value.push(value.data.data)
					goBottom()
				}
			}
		})
	})
</script>

<style lang="scss">
	uni-page-body {
		height: 100%;
	}
.scroll {
	height: calc(100% - 100rpx);
	padding: 20rpx 0;
	box-sizing: border-box;
	
	.news-item {
		display: flex;
		justify-content: flex-start;
		align-items: flex-start;
		padding: 0 20rpx;
		box-sizing: border-box;
		margin-bottom: 20rpx;
		.avatar {
			width: 80rpx;
			height: 80rpx;
			border-radius: 50rpx;
			border: 1rpx solid #565656;
			background-color: transparent;
		}
		.news-content {
			padding-left: 20rpx;
			display: flex;
			flex-direction: column;
			justify-content: flex-start;
			align-items: flex-start;
			.news-sender {
				font-size: 22rpx;
				color: #606060;
			}
			.news-text {
				margin-top: 10rpx;
				background-color: #fff;
				padding: 10rpx 20rpx;
				border-radius: 8rpx;
				white-space: normal;
				font-size: 30rpx;
				width: auto;
			}
		}
		
		&.self {
			flex-direction: row-reverse;
			.news-content {
				padding-right: 20rpx;
				padding-left: 0;
				.news-text {
					background-color: #DEE9FF;
				}
			}
		}
	}
}

.chat-footer {
	position: absolute;
	height: 100rpx;
	width: 100%;
	background-color: #f0f0f0;
	bottom: 0;
	left: 0;
	display: flex;
	justify-content: center;
	align-items: center;
	padding: 20rpx;
	box-sizing: border-box;
	z-index: 999;
}
</style>
